// pages/SignUps/SignUps.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList: [],
    index: 0,
    activityIndex: 0,
    activity: [],
    activityList:[],
    periodsList: [],
    periodsIndex: 0,
    periodsItemid: '',
    areaid: -1,
    isActive: false,
    personList: [],
    selectPerson:[],
    travelId:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },
  onShow:function() {
    this.getCityList()
  },
  // 选择期数
  bindPickerPeriodsChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value,
      areaid: e.target.dataset.id
    })
    this.selectActivity()
  },
  bindPickerActivity: function(e) {
    var that = this
    var num = Number(e.detail.value)
    this.setData({
      activityIndex: e.detail.value,
      periodsItemid: e.target.dataset.id,
      price: that.data.activityList[num].price
    })
    this.selectPeriods()
  },
  // 获取城市
  getCityList: function() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/select_city.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading()
        var data = res.data
        var list = data.list
        if (data.status == "success") {
          for (var k in list) {
            if (list[k].areaid == wx.getStorageSync('areaid')) {
              that.setData({
                index:k,
                title: list[k].title
              })
              break;
            }
          }
          that.setData({
            cityList: data.list,
            areaid: wx.getStorageSync('areaid'),
          })
          that.selectActivity()
        }
      }
    })
  },
  // 选择活动
  selectActivity: function() {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/select_hd.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        areaid: that.data.areaid
      },
      success(res) {
        wx.hideLoading()
        var data = res.data
        if (data.status == "success") {
          var list = data.list
          var itemid = wx.getStorageSync('ids')
          console.log('我的itemid' + itemid)
          for (var k in list) {
            console.log(list[k].itemid)
              if (list[k].itemid == itemid) {
                console.log(k)
                that.setData({
                  activityIndex:k,
                  price: data.list[k].price,
                  periodsItemid: itemid,
                })
              }
          }
          that.setData({
            activityList: data.list
          })
  
          that.selectPeriods()
        }
      }
    })
  },
  // 选择期数
  selectPeriods: function() {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/select_qs.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        itemid: that.data.periodsItemid
      },
      success(res) {
        wx.hideLoading()
        var data = res.data
        if (data.status == "success") {
        var list =data.list
        var qsid = wx.getStorageSync('qsid')
          for (var k in list) {
            if (list[k].id == qsid) {
              that.setData({
                periodsList: data.list,
                couple:data.list[k].id,
                periodsIndex:k
              })
            }
          }
          that.setData({
            periodsList: data.list
          })
        }
      }
    })
  },
  // 增加出行人
  addTravelPerson: function() {
    var that = this
    if (this.data.selectPerson.length<1) {
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/select_connection.php',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data: {
        userid: wx.getStorageSync('userid')
      },
      success(res) {
        console.log(res)
        var data = res.data
        var listData = data.list
        if (data.status == "success") {
          if (listData.length > 0) {
            for (var k in listData) {
              listData[k].isSelect = false
            }
            console.log(listData)
            if (listData.length>0) {
              that.setData({
                personList: listData,
                isActive: true
              })
            } else {
              wx.navigateTo({
                url: "/pages/addition/addition"
              })
            }
  
          }

        }
      },
      fail(err) {
        console.log(err)
      }
    })
    } else {
      that.setData({
        isActive: true
      })
    }
  },
  // 选择出行人
  selectTravlePerson(e) {
    var index = e.currentTarget.dataset.index
    var travelData = this.data.personList
    if (travelData[index].isSelect) {
      travelData[index].isSelect = false
    } else {
      travelData[index].isSelect = true
    }
    var res = []
    var user_child_id = []
    for (var y in travelData) {
      if (travelData[y].isSelect) {
        user_child_id.push(travelData[y].userid)
        res.push(travelData[y])
      }
    }
    console.log(travelData)
    var total = res.length * this.data.price
    var travelId = String(user_child_id)
    this.setData({
      personList: travelData,
      selectPerson: res,
      total: total,
      travelId: travelId
    })

  },
  closeMask: function() {
    this.setData({
      isActive: false
    })
  },
  // 提交
  submit:function() {
    var _this = this
    var getUserid = wx.getStorageSync('userid')
    wx.getStorage({
      key: 'userid',
      success(res) {
        if (_this.data.selectPerson.length > 0) {
          wx.request({
            url: 'https://miniapp.odeman.com/summercamp/signup.php',
            data: {
              userid: res.data,
              city: _this.data.areaid,
              action: _this.data.periodsItemid,
              couple: _this.data.couple,
              user_child_id: _this.data.travelId
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              console.log(res.data)
              if (res.data.status == "error") {
                wx.showModal({
                  title: '',
                  content: res.data.msg,
                })
              } else {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/pay/pay?state=0&orderId=' + res.data.itemid
                  })
                }, 2000)
              }
            }
          })
        } else {
          wx.showModal({
            title: '',
            content: '请添加出行人',
            success(res) {
              if (res.confirm) {
                _this.addTravelPerson()
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
        _this.setData({
          userid: res.data
        })
      },
      fail(err) {
        wx.navigateTo({
          url: '/pages/login/login'
        })
        console.log('失败')
      }
    })
    
  }
})